package com.origin.uilibrary.xrecycleview.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.origin.uilibrary.setDebounceClickListener
import com.origin.uilibrary.xrecycleview.viewholder.BaseViewHolder

abstract class BaseRecycleAdapter<E>(
    list: MutableList<E>,
    context: Context,
    var itemClick: Boolean = true
) :
    RecyclerView.Adapter<BaseViewHolder>() {

    var mList: MutableList<E> = list
    var mContext: Context = context

    var mItemClickListener: OnItemClickListener<E>? = null
    var mItemLongClickListener: OnItemLongClickListener? = null
    var mTitleClickListener: OnTitleClickListener? = null
    var mTitleLongClickListener: OnTitleLongClickListener? = null

    /**
     * 判断该position对应的位置是要固定
     *
     * @param position adapter position
     * @return true or false
     */
    open fun isPinnedPosition(position: Int): Boolean = false

    override fun getItemId(position: Int): Long {
        return position.toLong();
    }

    interface OnItemClickListener<E> {
        fun onItemClick(position: Int, item: E, view: View? = null)
    }

    interface OnItemLongClickListener {
        fun onItemLongClick(position: Int)
    }

    interface OnTitleClickListener {
        fun onTitleClick(position: Int)
    }

    interface OnTitleLongClickListener {
        fun onTitleLongClick(position: Int)
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {

        val viewHolder: BaseViewHolder =
            getViewResource(LayoutInflater.from(mContext), viewType, parent)


        if (itemClick) {
            viewHolder.itemView.setDebounceClickListener(
                {


                    val position: Int = it.tag as Int
                    if (isPinnedPosition(position)) {

                        mTitleClickListener?.apply {
                            onTitleClick(position)
                        }
                    } else {

                        mItemClickListener?.apply {

                            onItemClick(position, mList[position], it)
                        }
                    }
                }
            )

            viewHolder.itemView.setOnLongClickListener {
                val position: Int = it.tag as Int

                if (isPinnedPosition(position)) {
                    mTitleLongClickListener?.apply {
                        onTitleLongClick(position)
                    }
                } else {
                    mItemLongClickListener?.apply {
                        onItemLongClick(position)
                    }
                }
                true
            }
        }


//        viewHolder.setIsRecyclable(false);//取消viewHolder的重用机制。没有这句话子布局subView会被重复添加。
        return viewHolder
    }

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        holder.itemView.tag = position
        setItemData(holder, mList[position], position)
    }

    abstract fun setItemData(holder: BaseViewHolder, item: E, position: Int)

    override fun getItemCount(): Int {
        return mList.size
    }

    /**
     * 添加item布局
     */
    abstract fun getViewResource(
        inflater: LayoutInflater,
        viewType: Int,
        parent: ViewGroup
    ): BaseViewHolder


    fun getItemView(layoutManager: RecyclerView.LayoutManager, position: Int): View {

        return layoutManager.findViewByPosition(position)!!

    }

    protected fun <VB : ViewDataBinding> loadDataBinding(
        inflater: LayoutInflater,
        parent: ViewGroup,
        layout: Int
    ): ViewDataBinding {
        return DataBindingUtil.inflate<VB>(
            inflater,
            layout,
            parent,
            false
        )
    }

    protected fun <VB : ViewDataBinding> loadDataBindingBaseViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup,
        layout: Int
    ): BaseViewHolder {
        return BaseViewHolder(
            DataBindingUtil.inflate<VB>(
                inflater,
                layout,
                parent,
                false
            )
        )
    }


}